/* eslint-disable */

import * as update from 'react-addons-update';
import {
  createReceiveReducer,
  compose
} from '../renderers/renderer';

const wrapIn = (path, obj) => {
  if (!path) {
    return obj;
  }

  const newObj = {};
  const paths = path.split('.');
  const last =  paths.pop();
  paths.reduce((prev, key) => prev[key] = {}, newObj)[last] = obj;
  return newObj;
};

export const createRequestReducer = (key = 'fetching', path = null) => (state, action) => update(state, wrapIn(path, {
  $merge: {
    ctx: action.payload || action.meta,
    [key]: true,
    invalid: false
  }
}));

export {
  createReceiveReducer,
  compose
};